﻿using System;
using System.Collections.Generic;
using System.Text;

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using shudiangis.Struct;

namespace shudiangis.Common
{
    public class UserPerprotyQuery
    {
        /// <summary>
        /// 获取IFeature
        /// </summary>
        /// <param name="pFeatureLayer">图层</param>
        /// <param name="strQueryWhere">查询条件(name ='A')</param>
        /// <returns></returns>
        public static IFeature GetFeatureByQueryData(IFeatureLayer pFeatureLayer,string strQueryWhere)
        {
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

            //查询获取图例
            IQueryFilter pQueryFilter = new QueryFilter();
            pQueryFilter.WhereClause = strQueryWhere;

            IFeatureCursor pFeatureCursor = pFeatureClass.Search(pQueryFilter, false);

            IFeature pFeature = pFeatureCursor.NextFeature();
            while (pFeature == null)
            {
                pFeature = pFeatureCursor.NextFeature();
            }

            return pFeature;
        }

        /// <summary>
        /// 获取ISelectionSet 
        /// </summary>
        /// <param name="pFeatureLayer">图层</param>
        /// <param name="strQueryWhere">查询条件(name ='A')</param>
        /// <returns></returns>
        public static ISelectionSet GetFeatureSetByQueryData(IFeatureLayer pFeatureLayer, string strQueryWhere)
        {
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;

            //查询获取图例
            IQueryFilter pQueryFilter = new QueryFilter();
            pQueryFilter.WhereClause = strQueryWhere;

            ISelectionSet selectionSet = pFeatureClass.Select(pQueryFilter, esriSelectionType.esriSelectionTypeIDSet,
                esriSelectionOption.esriSelectionOptionOnlyOne, null);

            return selectionSet;
        }

   
        /// <summary>
        /// 内存中创建记录IFeature
        /// </summary>
        /// <param name="featureClass"></param>
        public static void CreateNewFeature(IFeatureClass featureClass, TableInfo tabInfo)
        {
            if (featureClass.ShapeType != tabInfo.ShapeValue.GeometryType) 
            { 
                return;
            }

            IFeature feature = featureClass.CreateFeature();
            feature.Shape = tabInfo.ShapeValue;

            ISubtypes subtypes = (ISubtypes)featureClass;
            IRowSubtypes rowSubtypes = (IRowSubtypes)feature;
            if (subtypes.HasSubtype)
            {
                rowSubtypes.SubtypeCode = 1;
            }

            rowSubtypes.InitDefaultValues();

            int i = 0;
            foreach (string strField in tabInfo.FieldName)
            {
                feature.set_Value(feature.Fields.FindField(strField), tabInfo.FieldValue[i]);
                i++;
            }

            feature.Store();
        }

    }
}
